#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int fds[2];
    char buf[100];
    int len;

    if (pipe(fds) == -1)
    {
        perror("pipe() fail");
        exit(1);
    }

    //读stdin
    while (fgets(buf, 100 ,stdin))
    {
        len = strlen(buf);
        //写到管道
        if (write(fds[1], buf, len) != len)
        {
            perror("write to pipe");
            break;
        }
        memset(buf, 0x00, sizeof(buf));

        //读管道内容
        if((len = read(fds[0], buf, 100)) == -1)
        {
            perror("read pipe fail");
            break;
        }

        //写出屏幕
        if(write(1, buf, len) != len)
        {
            perror("write stdout");
            break;
        }
    }
    return 0;
}